<template>
  <div>
    <el-button @click="addLeveOne">添加一级分类</el-button>
    <br />
    <el-input
      placeholder="输入关键字进行搜索   "
      v-model="filterText"
    >
    </el-input>
    <br />
    <el-tree
      :data="menus"
      :props="defaultProps"
      :filter-node-method="filterNode"
      ref="tree"
    >
      <span class="custom-tree-node" slot-scope="{ node, data }">
            <span>---{{ node.label }}</span>
            <span>---{{ data.murl }}</span>
            <span>---{{ data.mpath }}</span>
        <span>
          <el-button type="text" size="mini" @click="() => edit(data)">
            edit
          </el-button>

          <el-button type="text" size="mini" @click="() => remove(node, data)">
            Delete
          </el-button>
        </span>
      </span>
    </el-tree>

    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="form" label-position="right">
        <el-form :model="form">
          <el-form-item label="菜单名称" label-width="100px">
            <el-input v-model="form.mname" autocomplete="off" />
          </el-form-item>
          <el-form-item label="菜单组件路径" label-width="100px">
            <el-input v-model="form.murl" autocomplete="off" />
          </el-form-item>
          <el-form-item label="菜单访问路径" label-width="100px">
            <el-input v-model="form.mpath" autocomplete="off" />
          </el-form-item>
          <el-form-item label="菜单图标" label-width="100px">
            <el-input v-model="form.micon" autocomplete="off" />
          </el-form-item>
        </el-form>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addORupdate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      menus: [],
      defaultProps: {
        label: "mname",
        children: "menuList",
      },
      dialogFormVisible: false,
      title: "添加一级菜单",
      form: {},
      filterText: "",
    };
  },
  created() {
    this.getMenus();
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    },
  },
  methods: {
    filterNode(value, data) {
      if (!value) return true;
      return data.mname.indexOf(value) !== -1;
    },
    getMenus() {
      this.$http.get(`http://127.0.0.1/menu/tree`).then((res) => {
        this.menus = res.data.menuTree;
      });
    },

    edit(data) {
      this.$http
        .get(`http://127.0.0.1/menu/getById/${data.mid}`)
        .then(({ data }) => {
          this.form = data.menu;
          this.title = "更新菜单";
          this.dialogFormVisible = true;
        });
    },
    remove(node, data) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http
            .delete(`http://127.0.0.1/menu/delete/${data.mid}`)
            .then(({ data }) => {
              this.$message({
                type: "success",
                message: "保存成功",
              });
              this.getMenus();
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    addLeveOne() {
      this.form = {};
      this.title = "添加一级菜单";
      this.dialogFormVisible = true;
    },
    addORupdate() {
      if (this.form.mid != null) {
        this.$http
          .post(`http://127.0.0.1/menu/updateById`, this.form)
          .then(({ data }) => {
            this.$message({
              type: "success",
              message: "更新成功",
            });
            this.dialogFormVisible = false;
            this.getMenus();
          });
      } else {
        this.form.mpid = 0;
        this.$http
          .put(`http://127.0.0.1/menu/save`, this.form)
          .then(({ data }) => {
            this.$message({
              type: "success",
              message: "保存成功",
            });
            this.dialogFormVisible = false;
            this.getMenus();
          });
      }
    },
  },
};
</script>
<style  scoped>
</style>